<template>
	<view class="cart" v-if="cart.length !== 0">
		<my-address></my-address>
		
		<view class="cart-title">
			<uni-icons type="shop" size="18"></uni-icons>
			<text class="cart-title-text">购物车</text>
		</view>

		<!-- 商品列表区域 -->
		<!-- uni-swipe-action和 uni-swipe-action-item 实现向右滑动 删除购物车商品-->
		<uni-swipe-action>
			<block v-for="(goods,i) in cart" :key="i">
				<uni-swipe-action-item :right-options="options" @click="swipeItemActionHandler(goods)">
					<my-goods :goods="goods" :show-radio="true" :show-num="true" @num-change="numChangeHandler"
						@radio-change="radioClickHandler"></my-goods>
				</uni-swipe-action-item>
			</block>

		</uni-swipe-action>
		
		<!-- 结算区域 -->
		<my-settle></my-settle>
	</view>
	
	<!-- 购物车内没有商品 -->
	<view class="cart-empty" v-else>
		<image src="/static/cart-empty.png" class="empty-image"></image>
		<view class="cart-empty-text">空空如也~</view>
	</view>
</template>

<script>
	import badgeMin from '@/mixins/tabbar-badge.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		mixins: [badgeMin],
		computed: {
			...mapState('m_cart', ['cart'])
		},
		data() {
			return {
				options: [{
					text: '删除', //显示的文本
					style: {
						backgroundColor: '#C00000' //按钮的颜色
					}
				}]
			};
		},
		methods: {
			...mapMutations('m_cart', ['updateGoodsRadioState', 'updateGoodsNumder', 'delectGoodsById']),
			radioClickHandler(e) {
				this.updateGoodsRadioState(e)
			},
			numChangeHandler(e) {
				this.updateGoodsNumder(e)
			},
			swipeItemActionHandler(goods) {
				this.delectGoodsById(goods.goods_id)

			}
		}
	}
</script>

<style lang="scss">
	.cart {
		padding-bottom: 50px;
	}
	.cart-title {
		height: 40px;
		display: flex;
		align-items: center;
		font-size: 14px;
		padding-left: 5px;
		border-bottom: 1px solid #efefef;

		.cart-title-text {
			margin-left: 10px;
		}
	}
	
	.cart-empty {
		display: flex;
		align-items: center;
		flex-direction: column;
		padding-top: 150px;
		
		.empty-image {
			width: 90px;
			height: 90px;
		}
		
		.cart-empty-text {
			font-size: 12px;
			color: gray;
			margin-top: 15px;
		}
	}
</style>